package com.bdqn.common;

import com.bdqn.pojo.BackendUser;
import com.bdqn.pojo.DevUser;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.AsyncHandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 自定义拦截器
 *
 * @author LILIBO
 * @since 2021-06-11
 */
public class SysInterceptor implements AsyncHandlerInterceptor {

    private Logger logger = Logger.getLogger(SysInterceptor.class);

    /**
     * 在请求路径之前
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();

        BackendUser backendUser = (BackendUser) session.getAttribute(Constants.USER_SESSION);
        DevUser devUser = (DevUser) session.getAttribute(Constants.DEV_USER_SESSION);

        if (backendUser == null && devUser == null) {
            // 未登录时不能访问受保护的资源，跳转到403（权限不足）页面
            response.sendRedirect(request.getContextPath() + "/403.jsp");
            return false;
        }
        return true;
    }
}
